using AutoMapper;
using MediatR;
using Zhaoxi.Zhihu.Core.AppUserAggregate.Entites;

using Zhaoxi.Zhihu.SharedKerel.Messaging;
using Zhaoxi.Zhihu.SharedKerel.Repositoy;
using Zhaoxi.Zhihu.SharedKerel.Result;
using Zhaoxi.Zhihu.UseCases.Common.Interface;

namespace Zhaoxi.Zhihu.UseCases.AppUsers.Commands;

public record CreateAppUserCommand(int UserId) : ICommand<Result<CreatedAppUserDto>>;

public class CreateAppUserCommandHandler(
    IRepository<AppUser> userRepo,
    IMapper mapper):ICommandHandler<CreateAppUserCommand,Result<CreatedAppUserDto>>
{
    public async Task<Result<CreatedAppUserDto>> Handle(CreateAppUserCommand command, CancellationToken cancellationToken)
    {
        var user = userRepo.Add(new AppUser(command.UserId)
        {
            Nickname=$"新用户{command.UserId}"
        });
        
        await userRepo.SaveChangesAsync(cancellationToken);

        return Result.Success(mapper.Map<CreatedAppUserDto>(user));
    }
}

